<?php

namespace App\Http\Controllers;


use App\Scan;

class WelcomeController extends Controller
{
    public function index()
    {
        $app = app('wechat.official_account');

        $result = $app->qrcode->forever('szmz');
        $url = $app->qrcode->url($result['ticket']);

        $content = file_get_contents($url); // 得到二进制图片内容
        
        file_put_contents(__DIR__ . '/code.jpg', $content); // 写入文件
    }

    public function serve()
    {
        $app = app('wechat.official_account');
        $userApi = $app->user;
        $app->server->push(function($message)  use ($userApi) {
            $user = $userApi->get($message['FromUserName']);
            \Log::info($message);
            switch ($message['MsgType']) {
                case 'event':
                    if($message['Event'] == 'SCAN') {
                        if($message['EventKey'] == 'szmz') {
                            Scan::create([
                                'title' => $message['EventKey'],
                                'type'  => 1,
                                'open_id' => $message['FromUserName']
                            ]);
                            return '欢迎关注禧程。禧程网和深圳市民政局将在9月22日联合举办便民活动，推介内容为：一带一路大美禧程之格鲁吉亚。凡报名参加的朋友都将获得丰厚礼品，现场还有大奖抽出，奖品包括格鲁吉亚珍藏红酒、全球婚拍520元现金礼券、马尔代夫豪华酒店套房等等，奖品总额超万元。有兴趣的朋友千万不要错过，名额有限，马上添加客服禧禧的微信报名吧。微信号：xichengGZ';
                        }
                    }
                    if($message['Event'] == 'subscribe') {
                        if($message['EventKey'] == 'qrscene_szmz') {
                            Scan::create([
                                'title' => $message['EventKey'],
                                'type'  => 0,
                                'open_id' => $message['FromUserName']
                            ]);
                            return '欢迎关注禧程网，我們会为您定制与众不同的全球婚紗旅拍蜜月旅行，將您的想法告訴我们，让我们为您实现梦想之旅。客服微信：xichengGZ';
                        }
                    }
                    return '你好,'.$user['nickname'].', 欢迎关注禧程，我们会定期提供全球各地最in的婚庆旅拍蜜月旅游资讯，让禧禧带您一起浪！';
                    break;
                case 'text':
                    return '你好,'.$user['nickname'];
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                // ... 其它消息
                default:
                    return '欢迎关注';
                    break;
            }
        });

        return $app->server->serve();
    }

    /**
     * 获取素材列表
     * @return array
     */
    public function media()
    {
        $app = app('wechat.official_account');
        $media = $app->material;
        return $media->lists('news', 0, 20);

    }

    /**
     * 添加菜单
     */
    public function menu()
    {
        $app = app('wechat.official_account');
        $menu = $app->menu;
        $buttons = [
            [
                "name"       => "目的地",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "亚洲各地",
                        "url"  => "http://www.bigdayforus.com/wxcity/5"
                    ],
                    [
                        "type" => "view",
                        "name" => "澳洲",
                        "url"  => "http://www.bigdayforus.com/wxcity/4"
                    ],
                    [
                        "type" => "view",
                        "name" => "台湾香港",
                        "url"  => "http://www.bigdayforus.com/wxcity/3"
                    ],
                    [
                        "type" => "view",
                        "name" => "欧洲",
                        "url"  => "http://www.bigdayforus.com/wxcity/2"
                    ],
                    [
                        "type" => "view",
                        "name" => "美国",
                        "url"  => "http://www.bigdayforus.com/wxcity/1"
                    ],

                ],
            ],
            [
                "name"       => "最近活动",
                "sub_button" => [
                    [
                        "type"      => "media_id",
                        "name"      => "优惠活动",
                        "media_id"  => "bZHcVHq7neueSYOCmEc2C4z_wzAZ2pE79M3a0nf6rGk"
                    ],
                    [
                        "type"      => "media_id",
                        "name"      => "各城信息",
                        "media_id"  => "bZHcVHq7neueSYOCmEc2C_M0RrfV54pMMg-JN-fXTCY"
                    ],
                    [
                        "type"      => "media_id",
                        "name"      => "走心婚礼",
                        "media_id"  => "bZHcVHq7neueSYOCmEc2C9RBIJBEaZYH-VbkmwwCGcY"
                    ],
                ],
            ],
            [
                "name"       => "关于禧程",
                "sub_button" => [
                    [
                        "type"      => "media_id",
                        "name"      => "选择禧程",
                        "media_id"  => "bZHcVHq7neueSYOCmEc2C691e_OUW2vWTQofgcdkEQ0"
                    ],
                    [
                        "type"      => "media_id",
                        "name"      => "服务流程",
                        "media_id"  => "bZHcVHq7neueSYOCmEc2C5S1U8iO3narotal-vYx-p8"
                    ],
                    [
                        "type"      => "media_id",
                        "name"      => "人工客服",
                        "media_id"  => "bZHcVHq7neueSYOCmEc2CwUaqJTCxA1fskZNg2Cgtns"
                    ],
                ],
            ],
        ];
        $menu->create($buttons);
    }
}
